package com.ServiceHandle.Soap;

import java.io.IOException;
import java.net.SocketException;
import java.util.List;

import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;



import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.stork.cms.model.LoginDetails;
import com.stork.cms.model.mypassword;
import com.stork.cms.model.updatepasswordcode;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class ServiceCall {
	
	
	public ServiceCall() {
		// TODO Auto-generated constructor stub
	}
	
	
	 
	private static final String SOAP_ACTION1 = "http://tempuri.org/loginstudentid";
    private static final String METHOD_NAME1 = "loginstudentid";
    
	private static final String SOAP_ACTION2 = "http://tempuri.org/generatepassword";
    private static final String METHOD_NAME2 = "generatepassword";
    
    private static final String SOAP_ACTION3 = "http://tempuri.org/updatepassword";
	private static final String METHOD_NAME3 = "updatepassword";

    private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:2341/smsservice/webmethods.asmx";
   
    
    
   
    SoapObject request1 = new SoapObject(NAMESPACE, METHOD_NAME1);
    SoapObject request2 = new SoapObject(NAMESPACE, METHOD_NAME2);
    SoapObject request3 = new SoapObject(NAMESPACE, METHOD_NAME3);
    
    PropertyInfo properties=new PropertyInfo();
    PropertyInfo properties1=new PropertyInfo();
    
    HttpTransportSE httpTransport = new HttpTransportSE(URL,1000*60);
    
    
    String response1=null;
    String response2=null;
    String response3=null;
    String code=null;
    
    GsonBuilder gsonbuilder=new GsonBuilder();
    Gson gson=null;
    
    String getresponse=null;
    String getstudentid=null;
    String getpassword=null;
    String getupdatepassword=null;
    String notconnect="000";
   
    
    public String Studendloginid(String studentid,String password)
    {
    	properties.name="studentid";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request1.addProperty(properties,studentid);
       	
       
       	properties1.name="password";
       	properties1.type=PropertyInfo.STRING_CLASS;
       	request1.addProperty(properties1,password);
       try
       {
    	   
       	SoapSerializationEnvelope envelope1 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope1.dotNet = true;

        envelope1.setOutputSoapObject(request1);
        httpTransport.call(SOAP_ACTION1, envelope1);
        SoapPrimitive result1=(SoapPrimitive) envelope1.getResponse();
        Log.i("result111111111111", result1.toString());
        response1=result1.toString();
        
        gson=gsonbuilder.create();
        LoginDetails  logindetails=gson.fromJson(response1, LoginDetails.class);
        //Log.w("LoginDetails",logindetails.getObjcetcode());
        System.out.println("Valuee"+logindetails.getObjcetcode());
        getstudentid =logindetails.getObjcetcode();
       }
       catch(SocketException se)
       {
    	  
    	   System.out.println("socketttttt"+se.toString());
    	  
    	   return notconnect;
       }
       catch(XmlPullParserException xml)
       {
    	   System.out.println("xmllllllllll"+xml.toString());
    	   return notconnect;
       }   
       catch (IOException e) {
		// TODO Auto-generated catch block
    	   System.out.println("Inputttttttttt"+e.toString());
    	    return notconnect;
	}
        return getstudentid;
    }
    public String Getpassword(String username)
    {
    	properties.name="username";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request2.addProperty(properties,username);
       	
       
       	
       try
       {
    	   
       	SoapSerializationEnvelope envelope2 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope2.dotNet = true;

        envelope2.setOutputSoapObject(request2);
        httpTransport.call(SOAP_ACTION2, envelope2);
        SoapPrimitive result2=(SoapPrimitive) envelope2.getResponse();
        Log.i("result111111111111", result2.toString());
        response2=result2.toString();
        
        gson=gsonbuilder.create();
        
        mypassword logindetails=gson.fromJson(response2, mypassword.class);
       // mypasswordcode passwordcode=gson.fromJson(response2, mypasswordcode.class);
        Log.w("class call", "callllllllllllllllllllllllllllll");
        //Log.w("LoginDetails",passwordcode.toString());
        //Log.w("LoginDetails111111111111",logindetails.getForgetpassword().toString());
        
        getpassword =logindetails.getForgetpassword().get(0).getPassword();
        System.out.println("valueeeeeeeee"+getpassword);
        code=logindetails.getForgetpassword().get(0).getCode();
        System.out.println("codeeeeeeeeee"+code);
        
       
       
       }
       catch(SocketException se)
       {
    	   se.printStackTrace();
    	   return notconnect;
       }
       catch(XmlPullParserException xml)
       {
    	   xml.printStackTrace();
    	   return notconnect;
       } catch (IOException e) {
		// TODO Auto-generated catch block
    	   e.printStackTrace();
    	   return notconnect;
	}
       
       if(getpassword!=null)
       {
    	
				
			
       return getpassword;
       }
       else if(code.equals("444"))
       {
    	  
				
				
       	return code;
       }
       else if(code.equals("404"))
       {
    	  
				
				
       	return code;
       }
       String data=code.concat(getpassword);
       System.out.println("dataaaaaaaaa"+data);
       return data;
    }
    public String UpdatePassword(String username,String password)
    {
    	properties.name="username";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request3.addProperty(properties,username);
       	
       	properties1.name="password";
       	properties.type=PropertyInfo.STRING_CLASS;
       	request3.addProperty(properties1,password);
       
       	
       try
       {
    	   
       	SoapSerializationEnvelope envelope3 = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope3.dotNet = true;

        envelope3.setOutputSoapObject(request3);
        httpTransport.call(SOAP_ACTION3, envelope3);
        SoapPrimitive result3=(SoapPrimitive) envelope3.getResponse();
        Log.i("result333333333333", result3.toString());
        response3=result3.toString();
        
        gson=gsonbuilder.create();
        
        updatepasswordcode logindetails=gson.fromJson(response3, updatepasswordcode.class);
       // mypasswordcode passwordcode=gson.fromJson(response2, mypasswordcode.class);
        Log.w("class call", "callllllllllllllllllllllllllllll");
        //Log.w("LoginDetails",passwordcode.toString());
        //Log.w("LoginDetails111111111111",logindetails.getForgetpassword().toString());
        
        getupdatepassword =logindetails.getCodepassword();
        System.out.println("updatepassworddddddd"+getupdatepassword);
        
        
       
       
       }
       catch(SocketException se)
       {
    	   se.printStackTrace();
    	   return notconnect;
       }
       catch(XmlPullParserException xml)
       {
    	   xml.printStackTrace();
    	   return notconnect;
       } catch (IOException e) {
		// TODO Auto-generated catch block
    	   e.printStackTrace();
    	   return notconnect;
	}
       
       if(getupdatepassword.equals("111"))
       {
    	
				
			
       return getupdatepassword;
       }
       else if(getupdatepassword.equals("444"))
       {
    	  
				
				
       	return getupdatepassword;
       }
       else if(getupdatepassword.equals("404"))
       {
    	  
				
				
       	return getupdatepassword;
       }
       String data=code.concat(getpassword);
       System.out.println("dataaaaaaaaa"+data);
       return data;
    }
        
}
